package org.swift.commons.deployer.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.swift.commons.util.StringUtils;

@SuppressWarnings("unchecked")
public class UserInfoImpl implements UserInfo
{
	private final Map<String, Object> userAttributes;

	public Map<String, Object> getUserAttributes() {
		return userAttributes;
	}

	public UserInfoImpl(Map<String, Object> attribs) {
		if(attribs!=null){
			this.userAttributes = attribs;
		}else{
			this.userAttributes = new HashMap<String, Object>();
		}
	}

	public UserInfoImpl(String username,Collection<String> permissions){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("username", username);
		map.put("permissions", permissions);
		this.userAttributes = map;
	}

	public boolean hasPermission(String permission)
	{
		return this.getPermissions().contains(permission);
	}

	public Collection<String> getPermissions() {
		Collection<String> pcol = new ArrayList<String>();
		Object permissions = this.userAttributes.get("permissions");
		if(permissions!=null){
			if(permissions instanceof HashSet){
				String content = ((HashSet<String>)permissions).iterator().next();
				String[] sa = content.split(",");
				for(int i=0;i<sa.length;i++){
					pcol.add(sa[i]);
				}
				return Collections.unmodifiableCollection(pcol);
			}else if(permissions instanceof String){
				String[] sa = ((String)permissions).split(",");
				for(int i=0;i<sa.length;i++){
					pcol.add(sa[i]);
				}
				return Collections.unmodifiableCollection(pcol);
			}else if(permissions instanceof Collection){
				return Collections.unmodifiableCollection(((Collection<String>) permissions));
			}
		}
		return pcol;
	}

	public String getUsername()
	{
		return (String) this.userAttributes.get("username");
	}

	public String getName()
	{
		return this.getUsername();
	}
	
	public Object get(String key){
		return this.userAttributes.get(key);
	}

	private Pattern createPattern(String resourceType, String scope, String action) {

		String scopeRegexp = (scope == null) ? "[-\\w]*\\." : scope + "\\.";
		String resourceTypeRegexp = (resourceType == null) ? "[-\\w]*\\." : resourceType + "\\.";
		String resourceRegexp = "([-\\w]*)\\.";
		String actionRegexp = (action == null) ? "[-\\w]*" : action;

		return Pattern.compile(scopeRegexp + resourceTypeRegexp + resourceRegexp + actionRegexp);
	}

	public String getResources(String resourceType, String scope, String action)
	{
		Collection<String> resources = new HashSet<String>();
		Pattern pattern = createPattern(resourceType, scope, action);

		Matcher matcher;
		Collection<String> permissions = this.getPermissions();
		for (String permission : permissions) {
			matcher = pattern.matcher(permission);
			if (matcher.matches()) {
				resources.add(matcher.group(1));
			}
		}
		return StringUtils.commaDelimitedStringFromCollection(resources);
	}

	public String getResources(String resourceType, String scope)
	{
		return this.getResources(resourceType, scope, null);
	}

	public String getResources(String resourceType)
	{
		return this.getResources(resourceType, null, null);
	}
}